#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}

const ll inf=1000000000000000000; 
//const ll inf=1000000000;
const ll mod=998244353;
//const ll mod=1000000007;
const double eps=1e-8;

const int N=10000005;
int n,m,cnt; 
int a[N],c[N],b[N];

void read()
{
	int A,B,C,M;
	cin >> A >> B >> C >> M;
	rep(i,1,n)
	{
		ll x=a[i-1];
		a[i]=(x*x%M*A+B*x+C)%M+1;
	}
}
bool vis[N];
bool check(double p)
{
	double x=0;
	rep(i,1,cnt)
	{
		if (b[i]==1) x=1-x;
		else x=1-p-x;
		if (x<0) return 0;
		x=2*p-x;
		if (x<0) x=0,vis[i+1]=1;
		else vis[i+1]=0;
	}
	return 1;
}
ll ksm(ll x,ll y)
{
	ll res=1;
	while (y)
	{
		if (y&1) res=res*x%mod;
		x=x*x%mod,y>>=1;
	}
	return res;
}
struct frac
{
	ll x,y;// x/y
	frac(){}
	frac(ll _x,ll _y){x=_x,y=_y;}
	inline ll val()
	{
		return x*ksm(y,mod-2)%mod;
	}
	bool operator <(const frac &o)
	{
		return x*o.y<o.x*y;
	}
};
frac max(frac x,frac y)
{
	if (x<y) return y;
	return x;
}
frac min(frac x,frac y)
{
	if (x<y) return x;
	return y;
}
frac solve(int l,int r)
{
	ll A=0,B=0;
	frac res=frac(1,0);
	rep(i,l,r)
	{
		B=mod-B+1,A=mod-A;
		if (b[i]>1) A+=mod-1;
		A%=mod,B%=mod;
		if (A!=0) res=min(res,frac(B,mod-A));
		B=mod-B,A=mod-A+2;
		A%=mod,B%=mod;
	}
	return res;
}

void work()
{
	cin >> n >> a[0];
	rep(i,1,n) c[i]=0;
	read();
	rep(i,1,n) c[a[i]]++;
	cnt=0;
	rep(i,1,n)
	{
		if (c[i]==0) continue;
		b[++cnt]=c[i];
	}
	double l=0,r=1,rans=0;
	while (r-l>eps)
	{
		double mid=(l+r)/2;
		if (check(mid)) l=mid,rans=mid;
		else r=mid;
	}
	check(rans);
	vis[1]=1,vis[cnt+1]=1;
	int las=1;
	frac ans=frac(1,0);
	rep(i,2,cnt+1)
	{
		if (vis[i]==1)
		{
			ans=min(ans,solve(las,i-1));
			las=i;
		}
	}
	cout << fixed << setprecision(7) << r << " " << ans.val() << endl;
}

int main()
{
	freopen("guess.in","r",stdin);
	freopen("guess.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int T;
	cin >> T;
	while (T--) work();
	return 0;
}
